package ah.szxy.service.impl;

import ah.szxy.service.BusinessService;
import io.seata.core.context.RootContext;
import io.seata.spring.annotation.GlobalTransactional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 下单操作(库存的扣减与订单的新增)
 *
 * @author caohaiyang
 * @create 2020-09-17 下午 06:29
 */
@Service
public class BusinessServiceImpl implements BusinessService {
    @Autowired
    private BusinessServiceApi businessServiceApi;

    private static Logger logger = LoggerFactory.getLogger(BusinessServiceImpl.class);

    @GlobalTransactional  //解决分布式事务问题
    @Override
    public void purchase(String userId, String prodcutNo, int count) {
        String xid = RootContext.getXID();
        System.out.println("全局事务Id = " + xid);

        logger.info("准备下单, 下单用户{}, 商品编号{}, 数量为{} ",userId,prodcutNo,count);
        //1.远程调用库存服务 ->库存的扣减
        businessServiceApi.deduct(prodcutNo, count);
        //2.远程调用订单服务 ->订单的新增
        businessServiceApi.createOrder(userId, prodcutNo, count);

        logger.info("远程调用库存删减, 创建订单方法成功, 下单成功!");
    }
}